package com.chunfeng.lerunapplication;

import android.app.Dialog;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;
import com.chunfeng.lerunapplication.dialog.VerifyDialog;
import com.chunfeng.lerunapplication.fragement.FragmentAdapt;
import com.chunfeng.lerunapplication.fragement.HomeFragment;
import com.chunfeng.lerunapplication.fragement.MeFragment;
import com.chunfeng.lerunapplication.fragement.OrderFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 导航页
 */
public class PageActivity extends AppCompatActivity {
    /**
     * 标题
     */
    private TextView title;
    /**
     * 分页对象
     */
    private ViewPager2 page;
    /**
     * 底部按钮
     */
    private BottomNavigationView menuBtn;
    /**
     * 键值映射
     */
    private Map<Integer, Integer> map = new HashMap<Integer, Integer>() {{
        put(0, R.id.menu_home);
        put(1, R.id.menu_order);
        put(2, R.id.menu_me);
    }};
    /**
     * 滑动事件
     */
    private ViewPager2.OnPageChangeCallback onPageChange = new ViewPager2.OnPageChangeCallback() {
        /**
         * 滑动事件联动按钮
         *
         * @param position 当前的页面
         */
        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            Integer rid = map.get(position);
            if (rid == null) {
                return;
            }
            menuBtn.setSelectedItemId(rid);
        }
    };
    /**
     * 选中事件
     */
    private NavigationBarView.OnItemSelectedListener listener = new NavigationBarView.OnItemSelectedListener() {
        /**
         * 当按钮选中时
         * @param item 所选项目
         * @return true 将项目显示为所选项目，如果不应选择该项目，则为 false
         */
        @Override
        public boolean onNavigationItemSelected(@NotNull MenuItem item) {
            int itemId = item.getItemId();
            //根据值找key
            List<Integer> ids = map.entrySet()
                    .stream()
                    .filter(entry -> entry.getValue().equals(itemId))//查找与值匹配的键
                    .map(Map.Entry::getKey)//拿key值
                    .collect(Collectors.toList());
            if (ids.isEmpty()) {
                return false;
            }
            page.setCurrentItem(ids.get(0));
            //动态标题
            title.setText(item.getTitle());
            return true;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page);
        initView();
        pageViewSetting();
    }

    /**
     * 页面切换设置
     */
    private void pageViewSetting() {
        //帧布局
        // fragment列表
        List<Fragment> fragmentList = Arrays.asList(
                new HomeFragment(),//主页
                new OrderFragment(),//订单
                new MeFragment(this));//我的
        page.setOffscreenPageLimit(fragmentList.size() - 1);
        //设置滑动事件
        page.registerOnPageChangeCallback(onPageChange);
        //设置选中事件
        menuBtn.setOnItemSelectedListener(listener);
        page.setAdapter(new FragmentAdapt(getSupportFragmentManager(), getLifecycle(), fragmentList));
    }

    /**
     * 初始化页面元素
     */
    private void initView() {
        //标题
        title = findViewById(R.id.Main_Title);
        //菜单按钮
        menuBtn = findViewById(R.id.Main_Menu);
        //核心切换界面的布局
        page = findViewById(R.id.Main_Page);
    }

    /**
     * 监听返回键
     */
    @Override
    public void onBackPressed() {
        VerifyDialog verifyDialog = new VerifyDialog(this)
                .setTitle("提示")//标题
                .setMessage("你真的要退出主程序吗?退出后需要重新登录!")//消息
                .setOkButtonOnClickListener(dialog -> finish())
                .setCancelButtonOnClickListener(Dialog::dismiss);
        verifyDialog.show();
    }
}